<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{ goods.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{ goods.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodsInfo :goods="goods" />
          <!-- 规格选择组件 -->
          <XtxSku :goods="goods" @change="goodSkuChangeFn" />
          <!-- 数量选择组件 -->
          <XtxCount v-model="count" :min="1" :max="goods.inventory" isLabel />
          <!-- 按钮组件 -->
          <XtxButton @click="addToCart()" type="primary" style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <Detail :goods="goods" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
          <GoodsHot :goodsId="goods.id" :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { findGoodsDetail } from '@/api/goods'
import { useRoute } from 'vue-router'
import { onMounted, ref } from 'vue-demi'
import { GoodsDetail } from '@/api/goods.types'
import XtxSku, { SkuEmit } from '@/components/Sku/index.vue'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import Detail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { useStore } from '@/store'
import { message } from '@/components/Message'
import { CartItem } from '@/store/modules/cart'

const goods = ref<GoodsDetail>()
const route = useRoute()
const { id } = route.params as { id: string }
onMounted(async () => {
  const { result } = await findGoodsDetail(id)
  goods.value = result
})

const goodSkuChangeFn = (val: SkuEmit) => {
  if (val.skuId) {
    for (const key in val) {
      if (goods.value) {
        // goods.value[key] = val[key]
        goods.value[key] = Reflect.get(val, key)
      }
    }
  }
}

// 商品数量
const count = ref(1)

const { cart } = useStore()
// 加入购物车按钮
const addToCart = async () => {
  if (!goods.value) return
  // 1. 没有sku规格数据
  if (!goods.value.skuId) return message({ text: '请选择商品规格' })
  // 2. 添加的不能超过库存
  if (count.value > goods.value.inventory) return message({ text: '库存超出了' })
  // 3. 收集商品信息 - 💥 注意：后端接口字段命名不规范，要注意字段名匹配
  const goodsObj: CartItem = {
    id: goods.value.id, // 商品id
    name: goods.value.name, // 商品名称
    picture: goods.value.mainPictures[0], // 商品图片
    skuId: goods.value.skuId, // 有效skuId
    price: goods.value.oldPrice, // 原价格
    nowPrice: goods.value.price, // 现价格
    attrsText: goods.value.specsText, // 规格文案
    stock: goods.value.inventory, // 库存
    isEffective: true, // 是否有效商品
    selected: true, // 是否选中
    count: count.value // 商品数量
  }
  // 调用添加
  const text = await cart.addCart(goodsObj)
  // 添加成功提示
  message({ type: 'success', text: text })
  // 库存减一
  goods.value.inventory -= count.value
}
</script>

<style scoped lang="scss">

.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
